package it.valeandre.omino.fragment;

import it.valeandre.omino.R;
import it.valeandre.omino.adapter.StatisticsAdapter;
import it.valeandre.omino.model.Match;
import it.valeandre.omino.utils.Maps;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.Utils;

import java.util.Locale;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MatchStatisticsPageFragment extends BaseFragment {

    public static Fragment newInstance(Bundle matchData) {
        MatchStatisticsPageFragment myFragment = new MatchStatisticsPageFragment();
        myFragment.setArguments(matchData);
        return myFragment;
    }

    public static Fragment newInstance(Bundle matchData, OnFragmentTouchListener listener) {
        MatchStatisticsPageFragment myFragment = new MatchStatisticsPageFragment();
        myFragment.setArguments(matchData);
        myFragment.setOnFragmentTouchListener(listener);
        return myFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_match_detail_statistics, container, false);
        Bundle matchData = getArguments();
        Match match = (Match) matchData.getSerializable(ResBundle.KEY_BUNDLE_MATCH_DETAIL_DATA);
        fillHeadData(rootView, match);

        ListView statisticsList = (ListView) rootView.findViewById(R.id.fragment_match_detail_statistics_list);
        /*LinearLayout leftPanel = (LinearLayout) rootView.findViewById(R.id.fragment_match_detail_statistics_left_column);
        LinearLayout centerPanel = (LinearLayout) rootView.findViewById(R.id.fragment_match_detail_statistics_center_column);
        LinearLayout rightPanel = (LinearLayout) rootView.findViewById(R.id.fragment_match_detail_statistics_right_column);
        fillHomeData(leftPanel, match);
        fillCenterColumnData(centerPanel);
        fillAwayData(rightPanel, match);*/
        StatisticsAdapter adapter = new StatisticsAdapter(getActivity(), match);
        statisticsList.setAdapter(adapter);

        //ScrollView scroll = (ScrollView) rootView.findViewById(R.id.fragment_match_detail_statistics_scroll);
        statisticsList.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(mOnFragmentTouchListener != null) {
                    mOnFragmentTouchListener.onFragmentTouch(motionEvent);
                }
                return false;
            }
        });
        return rootView;
    }

    private void fillHeadData(ViewGroup rootView, Match match) {
        String hometeamString = match.getHometeam() == null || match.getHometeam().trim().equals("") ? match.getHometeamRealtime() : match.getHometeam();
        String visitorteamString = match.getAwayteam() == null || match.getAwayteam().trim().equals("") ? match.getAwayteamRealtime() : match.getAwayteam();

        TextView matchDay = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_league_and_day);
        matchDay.setText(match.getLeague() + " " + getResources().getString(R.string.league_day) + " " + match.getRound());

        TextView matchDate = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_date);
        String formattedDate = Utils.convertGmtStringToString(match.getDate(), "yyyy-MM-dd'T'hh:mm:ssZ", Locale.getDefault());
        matchDate.setText(formattedDate);

        ImageView homeTeamLogo = (ImageView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_logo);
        Utils.setLogoByName(hometeamString, homeTeamLogo, Maps.LOGOS_DIMENSION_SMALL_ID);

        ImageView awayTeamLogo = (ImageView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_logo);
        Utils.setLogoByName(visitorteamString, awayTeamLogo, Maps.LOGOS_DIMENSION_SMALL_ID);

        TextView homeTeamName = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_team);
        homeTeamName.setText(hometeamString);

        TextView awayTeamName = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_team);
        awayTeamName.setText(visitorteamString);

        TextView homeTeamScore = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_score);
        homeTeamScore.setText(match.getHomeGoals());

        TextView awayTeamScore = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_score);
        awayTeamScore.setText(match.getAwayGoals());
    }

    private void fillHomeData(LinearLayout leftRootView, Match match) {

        TextView shoots = (TextView) leftRootView.findViewById(R.id.match_statistics_column_shoots);
        setText(match.getHomeShots(), shoots);

        TextView shootsOnTarget = (TextView) leftRootView.findViewById(R.id.match_statistics_column_shoots_on_target);
        setText(match.getHomeShotsOnTarget(), shootsOnTarget);

        TextView corner = (TextView) leftRootView.findViewById(R.id.match_statistics_column_corner);
        setText(match.getHomeCorners(), corner);

        TextView fouls = (TextView) leftRootView.findViewById(R.id.match_statistics_column_fouls);
        setText(match.getHomeFouls(), fouls);

        TextView firstHalfGoals = (TextView) leftRootView.findViewById(R.id.match_statistics_column_first_half_goals);
        setText(match.getHalftimeHomeGoals(), firstHalfGoals);

        TextView yellowCards = (TextView) leftRootView.findViewById(R.id.match_statistics_column_yellow_cards);
        setText(match.getHomeYellowCards(), yellowCards);

        TextView redCards = (TextView) leftRootView.findViewById(R.id.match_statistics_column_red);
        setText(match.getHomeRedCards(), redCards);

        setPanelStyle(leftRootView, 24, R.color.red_champions, Typeface.BOLD);
    }

    private void fillCenterColumnData(LinearLayout centerRootView) {
        TextView shoots = (TextView) centerRootView.findViewById(R.id.match_statistics_column_shoots);
        shoots.setText(R.string.shoots);
        TextView shootsOnTarget = (TextView) centerRootView.findViewById(R.id.match_statistics_column_shoots_on_target);
        shootsOnTarget.setText(R.string.shoots_on_target);
        TextView corner = (TextView) centerRootView.findViewById(R.id.match_statistics_column_corner);
        corner.setText(R.string.corner);
        TextView fouls = (TextView) centerRootView.findViewById(R.id.match_statistics_column_fouls);
        fouls.setText(R.string.fouls);
        TextView firstHalfGoals = (TextView) centerRootView.findViewById(R.id.match_statistics_column_first_half_goals);
        firstHalfGoals.setText(R.string.first_half_goals);
        TextView yellowCards = (TextView) centerRootView.findViewById(R.id.match_statistics_column_yellow_cards);
        yellowCards.setText(R.string.yellow_cards);
        TextView redCards = (TextView) centerRootView.findViewById(R.id.match_statistics_column_red);
        redCards.setText(R.string.red_cards);

        setPanelStyle(centerRootView, 18, R.color.grey_text, Typeface.NORMAL);
    }

    private void fillAwayData(LinearLayout rightRootView, Match match) {

        TextView shoots = (TextView) rightRootView.findViewById(R.id.match_statistics_column_shoots);
        setText(match.getAwayShots(), shoots);

        TextView shootsOnTarget = (TextView) rightRootView.findViewById(R.id.match_statistics_column_shoots_on_target);
        setText(match.getAwayShotsOnTarget(), shootsOnTarget);

        TextView corner = (TextView) rightRootView.findViewById(R.id.match_statistics_column_corner);
        setText(match.getAwayCorners(), corner);

        TextView fouls = (TextView) rightRootView.findViewById(R.id.match_statistics_column_fouls);
        setText(match.getAwayFouls(), fouls);

        TextView firstHalfGoals = (TextView) rightRootView.findViewById(R.id.match_statistics_column_first_half_goals);
        setText(match.getHalftimeAwayGoals(), firstHalfGoals);

        TextView yellowCards = (TextView) rightRootView.findViewById(R.id.match_statistics_column_yellow_cards);
        setText(match.getAwayYellowCards(), yellowCards);

        TextView redCards = (TextView) rightRootView.findViewById(R.id.match_statistics_column_red);
        setText(match.getAwayRedCards(), redCards);

        setPanelStyle(rightRootView, 24, R.color.red_champions, Typeface.BOLD);
    }

    private void setPanelStyle(LinearLayout panel, int textSize, int textColorResID, int textStyle) {
        int textColor = getResources().getColor(textColorResID);
        for (int i=0; i<panel.getChildCount(); i++) {
            TextView child = (TextView) panel.getChildAt(i);
            child.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
            child.setTypeface(Typeface.defaultFromStyle(textStyle));
            child.setTextColor(textColor);
        }
    }

    private void setText(String text, TextView textView) {
        if(text != null && !text.equals("")) {
            textView.setText(text);
        } else {
            textView.setText("-");
        }
    }

}
